//
//  CHSuperviseViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/3/20.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHSuperviseViewController.h"
#import "CHSuperviseCell.h"
#import "CHBaseViewController.h"
#import "CHSuperviseModel.h"
#import "CHUploadDataController.h"
#import "CHExceptionListController.h"
#import "CHDockingViewController.h"
@interface CHSuperviseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSArray *resultArr;
@end

@implementation CHSuperviseViewController
-(NSArray *)resultArr{
    if (!_resultArr) {
        _resultArr = [NSArray array];
    }
    return _resultArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = KRGB(239, 239, 249);
    self.title = @"监管";
    
    [self setupUI];
    
    [super showLoading:YES];
    
    
    [self getData];
    
    
    
}


-(void)setupUI{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH  - 64  - 49 ) style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = KRGB(239, 239, 249);
    tableView.sectionFooterHeight = 10;
    tableView.delegate = self;
    tableView.dataSource = self;
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getData];
    }];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    

    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 100)];
    footerView.backgroundColor = [UIColor whiteColor];
    UIImageView *iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 20, 20)];
    iconImgView.image = [UIImage imageNamed:@"warning"];
    [footerView addSubview:iconImgView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 10, UIScreenW - 55, 30)];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.text = @"异常企业管理";
    titleLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    [footerView addSubview:titleLabel];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(iconImgView.frame) +15, UIScreenW, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:0.1];
    [footerView addSubview:line];
    
    UILabel *checkLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, UIScreenW, 30)];
    checkLabel.text = @"点击查看详情";
    checkLabel.textColor = [UIColor blueColor];
    checkLabel.font = [UIFont systemFontOfSize:14];
    checkLabel.textAlignment = NSTextAlignmentCenter;
    [footerView addSubview:checkLabel];
    
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [footerView addGestureRecognizer:tap];
    self.tableView.tableFooterView = footerView;
    
}

-(void)tapGesture:(UITapGestureRecognizer *)tap{
    
    id obj = [LongriseRouter openURL:@"CHExceptionListController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
}




-(void)getData{
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
   
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovregulatorydata" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        NSLog(@"监管%@",result);
        self.resultArr = nil;
        [self.tableView.mj_header endRefreshing];
        [super showLoading:NO];
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            NSDictionary *dict = result[@"data"];
            float dockcomnum = [dict[@"dockcomnum"] floatValue];
            float maintenance = [dict[@"maintenance"] floatValue];
            float nodockcomnum = [dict[@"nodockcomnum"] floatValue];
            float nomaintenance = [dict[@"nomaintenance"] floatValue];
            float nouploadnum = [dict[@"nouploadnum"] floatValue];
            float uploadnum = [dict[@"uploadnum"] floatValue];
           
            
            
            CHSuperviseModel *model = [[CHSuperviseModel alloc] init];
            model.title = @"联网对接情况";
            model.leftTitle = @"已对接";
            model.rightTitle = @"未对接";
            model.iconName = @"network";
            model.leftPercent = dockcomnum;
            model.rightPercent = nodockcomnum;
 
            
            CHSuperviseModel *model1 = [[CHSuperviseModel alloc] init];
            model1.title = @"数据上传情况";
            model1.leftTitle = @"已上传";
            model1.rightTitle = @"未上传";
            model1.iconName = @"data";
            model1.leftPercent = uploadnum;
            model1.rightPercent = nouploadnum;
     
            
            CHSuperviseModel *model2 = [[CHSuperviseModel alloc] init];
            model2.title = @"上传数据质量";
            model2.leftTitle = @"满分企业";
            model2.rightTitle = @"非满分企业";
            model2.iconName = @"quality";
            model2.leftPercent = maintenance;
            model2.rightPercent = nomaintenance;
            self.resultArr = @[model,model1,model2];
          
            [self.tableView reloadData];
        }
        
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.resultArr.count;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHSuperviseCell *cell = [CHSuperviseCell cellForTableView:tableView];
    
    if (indexPath.section == 0) {

        cell.leftBlock = ^{

            id obj = [LongriseRouter openURL:@"CHDockingViewController"];
            if(obj && [obj isKindOfClass:[UIViewController class]])
            {
                UIViewController *vc = obj;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            //判断该类是否可以传参数
            if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
            {
                id<LongriseRouterDelegater> delegater = obj;
                if(delegater)
                {
                    
                    NSMutableDictionary *params = [NSMutableDictionary dictionary];
                    CHSuperviseModel *model = self.resultArr[indexPath.section];
                    params[@"docking"] = @"1";
                    params[@"title"] = model.title;
                    params[@"index"] = [NSString stringWithFormat:@"%zd",indexPath.section];
                    //进行传值，传入统一的block以供B进行回调
                    [delegater setParams:params block:^(id obj) {
                        
                        
                    }];
                }
            }
            
        };
        cell.rightBlock = ^{
            
            
            id obj = [LongriseRouter openURL:@"CHDockingViewController"];
            if(obj && [obj isKindOfClass:[UIViewController class]])
            {
                UIViewController *vc = obj;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            //判断该类是否可以传参数
            if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
            {
                id<LongriseRouterDelegater> delegater = obj;
                if(delegater)
                {
                    NSMutableDictionary *params = [NSMutableDictionary dictionary];
                    CHSuperviseModel *model = self.resultArr[indexPath.section];
                    params[@"docking"] = @"0";
                    params[@"title"] = model.title;
                    params[@"index"] = [NSString stringWithFormat:@"%zd",indexPath.section];
                    //进行传值，传入统一的block以供B进行回调
                    [delegater setParams:params block:^(id obj) {
                        
                        
                    }];
                }
            }
        };
    }else{
        
        cell.leftBtn.userInteractionEnabled = NO;
        cell.rightBtn.userInteractionEnabled = NO;
    }
  
    cell.model = self.resultArr[indexPath.section];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 130;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
//        id obj = [LongriseRouter openURL:@"CHDockingViewController"];
//        if(obj && [obj isKindOfClass:[UIViewController class]])
//        {
//            UIViewController *vc = obj;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//
//        //判断该类是否可以传参数
//        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
//        {
//            id<LongriseRouterDelegater> delegater = obj;
//            if(delegater)
//            {
//
//                NSMutableDictionary *params = [NSMutableDictionary dictionary];
//                CHSuperviseModel *model = self.resultArr[indexPath.section];
//                params[@"title"] = model.title;
//                params[@"index"] = [NSString stringWithFormat:@"%zd",indexPath.section];
//                //进行传值，传入统一的block以供B进行回调
//                [delegater setParams:params block:^(id obj) {
//
//
//                }];
//            }
//        }
    }else if (indexPath.section == 2) {
        
        
        id obj = [LongriseRouter openURL:@"CHUploadDataController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                params[@"index"] = [NSString stringWithFormat:@"%zd",indexPath.section];
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
    
    }else{
        
        id obj = [LongriseRouter openURL:@"CHBaseViewController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                CHSuperviseModel *model = self.resultArr[indexPath.section];
                params[@"title"] = model.title;
                params[@"index"] = [NSString stringWithFormat:@"%zd",indexPath.section];
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
